package com.ikas.ai.server.module.risk.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.aop.Pageable;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.risk.model.RiskPolicy;
import com.ikas.ai.server.module.risk.service.RiskPolicyService;
import com.ikas.ai.server.module.system.model.User;
import com.ikas.ai.utils.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 风险处置策略
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/1/31 11:54
 */
@RestController
@RequestMapping("/risk/policy")
public class RiskPolicyController extends AbstractBean {

    @Autowired
    private RiskPolicyService riskPolicyService;

    /**
     * 获取风险策略分页数据
     *
     * @param riskName 风险名称
     * @param systemNo 系统编码
     * @return JsonResult
     */
    @GetMapping("/page")
    @Pageable(sortType = "desc")
    public JsonResult pageQueryRiskPolicy(@RequestParam(value = "riskName", required = false) String riskName,
                                          @RequestParam(value = "systemNo", required = false) String systemNo,
                                          @RequestParam(value = "machineNo", required = false) String machineNo) {
        return JsonResult.ok(riskPolicyService.pageQueryRiskPolicy(riskName, systemNo, machineNo));
    }

    /**
     * 获取所有的风险策略
     *
     * @param name
     * @param systemNo
     * @return
     */
    @GetMapping("/getAllRiskPolicy")
    public JsonResult getAllRiskPolicy(@RequestParam(value = "name", required = false) String name,
                                       @RequestParam(value = "systemNo", required = false) String systemNo,
                                       @RequestParam(value = "machineNo", required = false) String machineNo) {
        return JsonResult.ok(riskPolicyService.pageQueryRiskPolicy(name, systemNo, machineNo));
    }

    /**
     * 获取风险处置策略，通过系统编号
     *
     * @param systemNo 系统编号
     * @return JsonResult
     */
    @GetMapping("/list/bySystemNo")
    public JsonResult getRiskPolicyBySystemNo(
            @RequestParam(value = "systemNo") String systemNo) {
        return JsonResult.ok(riskPolicyService.getRiskPolicyBySystemNo(systemNo));
    }


    /**
     * 新增风险策略信息
     *
     * @param param RiskPolicy
     * @return JsonResult
     */
    @PostMapping("/add")
    public JsonResult addRiskPolicy(@RequestBody RiskPolicy param) {
        User user = getUser();
        param.setCreator(user.getId());
        return JsonResult.ok(riskPolicyService.saveRiskPolicy(param));
    }

    /**
     * 修改风险策略信息
     *
     * @param param RiskPolicy
     * @return JsonResult
     */
    @PostMapping("/update")
    public JsonResult updateRiskPolicy(@RequestBody RiskPolicy param) {
        User user = getUser();
        param.setCreator(user.getId());
        return JsonResult.ok(riskPolicyService.updateRiskPolicy(param));
    }

    /**
     * 删除风险策略信息
     *
     * @param ids ids,多个用逗号隔开
     * @return JsonResult
     */
    @PostMapping("/{ids}")
    public JsonResult deleteRiskPolicy(@PathVariable(name = "ids") String ids) {
        String[] idsStr = ids.split(",");
        List<Long> idsInt = new ArrayList<>();
        for (String id : idsStr) {
            if (!NumUtil.isInteger(id)) {
                continue;
            }
            idsInt.add(Long.parseLong(id));
        }
        return JsonResult.ok(riskPolicyService.deleteRiskPolicy(idsInt));
    }

}
